package net.sf.xplore.ui.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.text.View;

import net.sf.xplore.ui.UiConstants.MenuContentColors;
import net.sf.xplore.ui.UiConstants.MenuTabColors;
import sun.swing.SwingUtilities2;

public class TabGradientUI extends BasicTabbedPaneUI {

	@Override
	protected void paintTabBorder(Graphics graphics, int tabPlacement, int tabIndex,
			int x, int y, int w, int h, boolean isSelected) {
		if(null == graphics)
			return;
		Graphics2D g = (Graphics2D) graphics;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		        RenderingHints.VALUE_ANTIALIAS_ON);
		
		g.setColor((isSelected ? MenuTabColors.SELECTED_OUTTER_BORDER : MenuTabColors.OUTTER_BORDER));
		g.drawRect(x, y, w, h);
		
		g.setColor((isSelected ? MenuTabColors.SELECTED_INNER_BORDER : MenuTabColors.INNER_BORDER));
		g.drawRect(x+1, y+1, w-1, h-1);
	}

	@Override
	protected void paintTabBackground(Graphics graphics, int tabPlacement,
			int tabIndex, int x, int y, int w, int h, boolean isSelected) {
		
		if(null == graphics)
			return;
		Graphics2D g = (Graphics2D) graphics;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		        RenderingHints.VALUE_ANTIALIAS_ON);
		
		Rectangle2D r = new Rectangle2D.Double(x+2, y+2, w-3, h-3);
		GradientPaint gp = new GradientPaint(
				0, 0,
				(isSelected ? MenuTabColors.SELECTED_BG_GRAD_TOP : MenuContentColors.BACKGROUND_GRAD_TOP), 
				0, h/2,
				(isSelected ? MenuTabColors.SELECTED_BG_GRAD_BOTTOM : MenuContentColors.BACKGROUND_GRAD_BOTTOM), 
				true);
		
		g.setPaint(gp);
		g.fill(r);
		
	}
	

	@Override
	protected void paintText(Graphics graphics, int tabPlacement, Font font,
			FontMetrics metrics, int tabIndex, String title,
			Rectangle textRect, boolean isSelected) {
		if(null == graphics)
			return;
		Graphics2D g = (Graphics2D) graphics;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		        RenderingHints.VALUE_ANTIALIAS_ON);
		
		g.setFont(font);

        View v = getTextViewForTab(tabIndex);
        if (v != null) {
            // html
            v.paint(g, textRect);
        } else {
            // plain text
            int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);

            if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
                Color fg = tabPane.getForegroundAt(tabIndex);
                if (isSelected && (fg instanceof UIResource)) {
                    Color selectedFG = MenuTabColors.SELECTED_FOREGROUND;
                    if (selectedFG != null) {
                        fg = selectedFG;
                    }
                }
                g.setColor(fg);
                SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                             title, mnemIndex,
                             textRect.x, textRect.y + metrics.getAscent());

            } else { // tab disabled
                g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
                SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                             title, mnemIndex,
                             textRect.x, textRect.y + metrics.getAscent());
                g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
                SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                             title, mnemIndex,
                             textRect.x - 1, textRect.y + metrics.getAscent() - 1);

            }
        }
	}

	
	
}
